package top.lilele.adminSystem.service;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.EnumUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.lilele.adminSystem.entity.AdminMapEntity;
import top.lilele.adminSystem.mapper.AdminMapMapper;
import top.lilele.adminSystem.numEnum.MapEnum;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lilele
 */
@Service
@Slf4j
public class AdminMapService {

    @Resource
    AdminMapMapper adminMapMapper;

    /**
     * 新增数据字典
     *
     * @param list
     */
    public void addMap(List<AdminMapEntity> list) {
        list.stream().forEach(item -> {
            // 新增
            if (item.getId() == null) {
                item.setCreatedId(StpUtil.getLoginIdAsString());
                adminMapMapper.insert(item);
            } else {
                item.setUpdatedId(StpUtil.getLoginIdAsString());
                adminMapMapper.updateById(item);
            }
        });
    }

    /**
     * 查询数据字典
     */
    public List<AdminMapEntity> queryMapList() {
        List<AdminMapEntity> list = adminMapMapper.selectList(null);
        return list;
    }

    /**
     * 更新数据字典
     *
     * @param adminMapEntity
     */
    public void updateMap(AdminMapEntity adminMapEntity) {
        LambdaUpdateWrapper<AdminMapEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(AdminMapEntity::getValue, adminMapEntity.getValue());
        updateWrapper.set(AdminMapEntity::getUpdatedId, StpUtil.getLoginIdAsString());
        updateWrapper.eq(AdminMapEntity::getId, adminMapEntity.getId());
        adminMapMapper.update(null, updateWrapper);
    }

    /**
     * 删除数据字典
     *
     * @param ids
     */
    public void deleteMap(List<String> ids) {
        adminMapMapper.deleteBatchIds(ids);
    }
}
